import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { message } from 'antd';
import { fetchValidation } from '@/utils';
import {
  queryBatchCommon,
  queryBatchDetail,
  queryBatchCount,
  batchDelete,
  downloadCode,
  modifyPhone,
  checkModifyPhone,
} from './service';


export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    fetchCommon: Effect;
    fetch: Effect;
    count: Effect;
    delete: Effect;
    download: Effect;
    checkModify: Effect;
    modify: Effect;
  };
  reducers: {
    save: Reducer;
    saveCommon: Reducer;
    cleanCount: Reducer;
  };
}

const Model: ModelType = {
  namespace: 'logisCenterBatchDetail',

  state: {
    common: {},
    data: {
      content: [],
      count: 0,
    },
  },

  effects: {
    * fetchCommon({ payload }, { call, put }) {
      const response = yield call(queryBatchCommon, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'saveCommon',
        payload: { type: 'common', value: data.body },
      });
    },
    * fetch({ payload }, { call, put }) {
      const { pageIndex, ...query } = payload.params;
      const response = yield call(queryBatchDetail, query);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: {
          type: 'list',
          value: {
            current: pageIndex,
            search: payload.search,
            list: data.body.list,
            maxId: payload.maxId,
            minId: payload.minId,
          },
        },
      });
    },
    * count({ payload }, { call, put }) {
      const { pageIndex, pageSize, ...query } = payload;
      const response = yield call(queryBatchCount, query);
      const data = fetchValidation(response);
      if (data) {
        yield put({
          type: 'save',
          payload: { type: 'count', value: data.body },
        });
      }
    },
    * delete({ payload }, { call, put }) {
      const response = yield call(batchDelete, payload.params);
      const data = fetchValidation(response);
      if (data) {
        message.success(data.msg);
        yield put({
          type: 'fetch',
          payload: { params: payload.query },
        });
      }
    },
    * download({ payload }, { call, put }) {
      yield call(downloadCode, payload);
    },
    * checkModify({ payload }, { call, put }) {
      const response = yield call(checkModifyPhone, payload);
      return fetchValidation(response);
    },
    * modify({ payload }, { call, put }) {
      const response = yield call(modifyPhone, payload.params);
      const data = fetchValidation(response);
      if (data) {
        message.success(data.msg);
        yield put({
          type: 'fetch',
          payload: { params: payload.query },
        });
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      const { data } = state;
      if (payload.type === 'list') {
        const pos = data.content.findIndex((item: any) => item.current === payload.value.current);
        if (pos === -1) {
          data.content.push(payload.value);
        } else {
          data.content[pos] = payload.value;
        }
      } else if (payload.type === 'count') {
        data.count = payload.value;
      }
      return {
        ...state,
        data,
      };
    },
    saveCommon(state, { payload }) {
      return {
        ...state,
        [payload.type]: payload.value,
      };
    },
    cleanCount(state) {
      const { data } = state;
      data.count = 0;
      return {
        ...state,
        data,
      };
    },
  },
};

export default Model;
